package cn.stronglink.asset.module.sys.department.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.Department;
import cn.stronglink.asset.module.sys.department.mapper.DepartmentMapper;
import cn.stronglink.asset.module.sys.department.vo.DepartmentTreeNodeVo;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class DepartmentService {

	@Autowired
	private DepartmentMapper departmentMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<DepartmentTreeNodeVo> getTree() {
		List<DepartmentTreeNodeVo> rootDept = departmentMapper.qryRootDept();	
		if(rootDept!=null&&rootDept.size()>0){
			for(DepartmentTreeNodeVo vo:rootDept){
				getChildren(vo);
			}
		}
        return rootDept;  
	}
	
	private void getChildren(DepartmentTreeNodeVo vo) {
		List<DepartmentTreeNodeVo> voList=departmentMapper.getChildList(vo.getId());
		if(voList!=null&&voList.size()>0){
			for(DepartmentTreeNodeVo child:voList){
				getChildren(child);
			}
			vo.setDepts(voList);
		}
		
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<Department> getDeptsByPid(Page<Department> page,Long pid,String name) {
		page.setRecords(departmentMapper.getDeptsByPid(page,pid,name));
		return page;	
	}
	
	public void insertDept(Department dept){
		if (dept.getId() == null) {
			dept.setId(IdWorker.getId());
			dept.setCreateBy(WebUtil.getCurrentUser());
			dept.setCreateTime(new Date());
			departmentMapper.insertDept(dept);		
		}else{
			dept.setUpdateBy(WebUtil.getCurrentUser());
			dept.setUpdateTime(new Date());
			departmentMapper.updateDept(dept);
		}	
	}
	
	public void updateDept(Department dept) {
		dept.setUpdateBy(WebUtil.getCurrentUser());
		dept.setUpdateTime(new Date());
		departmentMapper.updateDept(dept);	
	}
	
	public void removeDept(Long id){
		departmentMapper.delDept(id);	
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Department qryDeptById(Long id){
		return departmentMapper.qryDeptById(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public int getChildDetpCount(Long id) {
		return departmentMapper.getChildDetpCount(id);
	}  

	private void recursion(List<DepartmentTreeNodeVo> list, DepartmentTreeNodeVo node) {
		List<DepartmentTreeNodeVo> childList = getChildList(list, node);// 得到子节点列表
		if (!CollectionUtils.isEmpty(childList)) {
			node.setDepts(childList);
			Iterator<DepartmentTreeNodeVo> it = childList.iterator();
			while (it.hasNext()) {
				DepartmentTreeNodeVo n = (DepartmentTreeNodeVo) it.next();
				recursion(list, n);
			}
		} else {
			node.setDepts(null);
		}
	}
	
	private List<DepartmentTreeNodeVo> getChildList(List<DepartmentTreeNodeVo> list, DepartmentTreeNodeVo node) {  
        List<DepartmentTreeNodeVo> nodeList = new ArrayList<DepartmentTreeNodeVo>();  
        Iterator<DepartmentTreeNodeVo> it = list.iterator();  
        while (it.hasNext()) {  
        	DepartmentTreeNodeVo n = (DepartmentTreeNodeVo) it.next();  
            if (n.getPid().equals(node.getId()) ) {  
                nodeList.add(n);  
            }  
        }  
        return nodeList;  
    }

	

	

}
